#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstdlib>
#include <cstring>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#include "Log.hpp"

#define BACKLOG 5

class TcpServer
{
    private:
        int port;
        int listen_sock;
        static TcpServer* svr;
    private:
        TcpServer(int _port)
           :port(_port)
           ,listen_sock(-1)
        {}
        //拷贝构造和赋值运算符重载函数私有化
        TcpServer(const TcpServer& s) = delete;
        TcpServer& operator=(const TcpServer& s) = delete;
    public:
        //设置为单例模式
        static TcpServer* getInstance(int port)
        {
            //pthread_mutex_initializer 
            static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
            if(svr == nullptr)
            {
                pthread_mutex_lock(&lock);
                if(svr == nullptr)
                {   
                    //创建TcpServer对象
                    svr = new TcpServer(port);
                    //初始化TcpServer
                    svr->InitServer();
                }
                pthread_mutex_unlock(&lock);
            }
            return svr;
        }

        //初始化TCP服务器: socket(); bind(); listen();
        void InitServer()
        {
             Socket();
             Bind();
             Listen();
             LOG(INFO,"tcpserver init ... success");
        }

        //创建套接字
        void Socket()
        {
            //创建套接字
            listen_sock = socket(AF_INET,SOCK_STREAM,0);
            //listen_sock < 0 表示创建套接字失败
            if(listen_sock < 0)
            {
                LOG(FATAL,"create socket error!");
                exit(1);
            }
            int opt = 1;
            //设置地址重用选项 目的:避免主动断开连接的一方进入TIME_WAIT状态后，无法重新占用此端口
            setsockopt(listen_sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
            LOG(INFO,"create socket ... success");
        }
        //绑定地址信息
        void Bind()
        {
            struct sockaddr_in local;
            memset(&local,0,sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = INADDR_ANY;
            if(bind(listen_sock,(struct sockaddr*)&local,sizeof(local)) < 0)
            {
                LOG(FATAL,"bind socket error!");
                exit(2);
            }
            LOG(INFO,"bind socket ... success");
        }
        //设置监听
        void Listen()
        {
            //BACKLOG == 5
            if (listen(listen_sock,BACKLOG) < 0 )
            {
                LOG(FATAL,"listen socket error!");
                exit(3);
            }
            LOG(INFO,"listen socket ... succsess");
        }
        //获取套接字描述符
        int sock()
        {
            return listen_sock;
        }

        ~TcpServer()
        {
             if(listen_sock >= 0)
             {
                 close(listen_sock);
             }
        }
};
//静态成员变量类外初始化
TcpServer* TcpServer::svr = nullptr;
